/**************************************************************************/
/*!
  @file     CST816T.h
  Author: Atsushi Sasaki(https://github.com/aselectroworks)
  License: MIT (see LICENSE)
*/
/**************************************************************************/

#ifndef _CST816T_H
#define _CST816T_H

#include <Arduino.h>
#include <stdint.h>
#include <stdbool.h>

#define TouchWidth 240
#define TouchHeight 280

#define I2C_ADDR_CST816T 0x15

#define CHIPID_CST716 0x20
#define CHIPID_CST816S 0xB4
#define CHIPID_CST816T 0xB5
#define CHIPID_CST816D 0xB6

// 触摸旋转方向
typedef enum
{
    Rotation_0 = 0,
    Rotation_1,
    Rotation_2,
    Rotation_3,
} TouchRotation;

// #define GESTURE_NONE 0x00
// #define GESTURE_SWIPE_UP 0x01
// #define GESTURE_SWIPE_DOWN 0x02
// #define GESTURE_SWIPE_LEFT 0x03
// #define GESTURE_SWIPE_RIGHT 0x04
// #define GESTURE_SINGLE_CLICK 0x05
// #define GESTURE_DOUBLE_CLICK 0x0B
// #define GESTURE_LONG_PRESS 0x0C

typedef enum : uint8_t
{
    None = 0x00,
    SlideDown = 0x01,
    SlideUp = 0x02,
    SlideLeft = 0x03,
    SlideRight = 0x04,
    SingleTap = 0x05,
    DoubleTap = 0x0B,
    LongPress = 0x0C
} Gestures;

typedef struct
{
    uint16_t x = 0;
    uint16_t y = 0;
    Gestures gesture = Gestures::None;
    bool touching = false;
    bool isValid = false;
} TouchInfos;

enum touchpad_mode
{
    mode_touch,
    mode_change,
    mode_fast,
    mode_motion
};

/**************************************************************************/
/*!
    @brief  CST816T I2C CTP controller driver
*/
/**************************************************************************/
class CST816T
{
public:
    CST816T();
    CST816T(uint8_t rst_n, uint8_t int_n);
    CST816T(uint8_t sda, uint8_t scl, uint8_t rst_n, uint8_t int_n);
    virtual ~CST816T();

    void begin(void);
/*
   mode_touch: interrupt every 10ms when a touch is detected
   mode_change: interrupt when a touch change is detected
   mode_fast: interrupt when a click or swipe is detected
   mode_motion: interrupt when a click, swipe, double click, or long press is detected
*/
    void begin(touchpad_mode tp_mode, bool int_enable); // +
    void setRotation(TouchRotation Rotation);

    // Scan Function
    TouchInfos GetTouchInfo(void);

    bool available(); // +
    String state(TouchInfos _info);   // +
	
    uint8_t chip_id;  // +

private:
    int sda = -1;
    int scl = -1;
    uint8_t int_n = -1;
    uint8_t rst_n = -1;
    uint8_t touch_rotation = Rotation_0;

    // Unused/Unavailable commented out
    static constexpr uint8_t gestureIndex = 1;
    static constexpr uint8_t touchPointNumIndex = 2;
    // static constexpr uint8_t touchEventIndex = 3;
    static constexpr uint8_t touchXHighIndex = 3;
    static constexpr uint8_t touchXLowIndex = 4;
    // static constexpr uint8_t touchIdIndex = 5;
    static constexpr uint8_t touchYHighIndex = 5;
    static constexpr uint8_t touchYLowIndex = 6;
    // static constexpr uint8_t touchStep = 6;
    // static constexpr uint8_t touchXYIndex = 7;
    // static constexpr uint8_t touchMiscIndex = 8;

    uint8_t readByte(uint8_t addr);
    void writeByte(uint8_t addr, uint8_t data);
};
#endif